import java.util.Arrays;
import java.util.PriorityQueue;

public class SolutionLCP30 {
    public int magicTower(int[] nums) {
        if (Arrays.stream(nums).sum()<0){
            return -1;
        }
        PriorityQueue<Integer> priorityQueue=new PriorityQueue<>((a,b)->b-a);
        long now=0;
        int ans=0;
        for (int i=0;i<nums.length;i++){
            now+=nums[i];
            if (nums[i]<0){
                priorityQueue.offer(-nums[i]);
                if (now<0){
                    int t=priorityQueue.poll();
                    now+=t;
                    ans++;
                }
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new SolutionLCP30().magicTower(new int[]{100,100,100,-250,-60,-140,-50,-50,100,150}));
    }
}
